#!/bin/bash


export PATH=/usr/bin:/bin:/usr/sbin

if [ ! -x  /usr/xpg4/bin/grep ]; then 
	echo "/usr/xpg4/bin/grep is missing or not executable."
	exit 1
fi


printf "%-60s" "Verify package SUNWbindr is installed..."
pkginfo SUNWbindr >/dev/null  2>&1
if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s" "Verify package SUNWbind is removed..."
pkginfo SUNWbind >/dev/null  2>&1
if [ $? -ne 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s" "Verify /etc/default/login..."
/usr/xpg4/bin/grep -q '^SUPATH=/opt/bind/bin:/opt/bind/sbin:/usr/sbin:' /etc/default/login 2>/dev/null
if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s" "Verify /.profile..."
/usr/xpg4/bin/grep -q "^PATH=/opt/bind/sbin:/opt/bind/bin:/usr/local/sbin:" /.profile 2>/dev/null
if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s"  "Verify user named..."
if [ "X$(id named 2>/dev/null)" ==  "Xuid=1688(named) gid=1688(named)" ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s" "Verify /etc/user_attr..."
/usr/xpg4/bin/grep -q  '^named::::type=normal;defaultpriv=basic,!proc_session,!proc_info,!file_link_any,net_privaddr,file_dac_read,file_dac_search,sys_resource,proc_chroot' /etc/user_attr
if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s" "Verify /.ssh/authorized_keys..."
grep "^ssh-dss AAAAB3NzaC1kc3MAAACBANXNZKyitH2i97lkYexvoVSYtrtYMQBGhrSUcnD6vw8Uam66VWW" /.ssh/authorized_keys | /usr/xpg4/bin/grep -q "root@sjcitdns01$"
if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s"  "Verify /lib/svc/method/dns-servers..."
if /usr/xpg4/bin/grep -q '^server="/opt/bind/sbin/named"' /lib/svc/method/dns-server -a /usr/xpg4/bin/grep -q "^[[:space:]]*configuration_file=/etc/named/named.conf" /lib/svc/method/dns-server  2>/dev/null ; then echo "Success"; else echo "Failure"; fi

printf "%-60s"  "Verify SMF start/user..."
svccfg -s svc:/network/dns/server:default listprop 2>/dev/null | /usr/xpg4/bin/grep -q  "^start\/user[[:space:]]\{1,\}astring[[:space:]]\{1,\}named"  2>/dev/null
if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s"  "Verify SMF start/group..."
svccfg -s svc:/network/dns/server:default listprop 2>/dev/null | /usr/xpg4/bin/grep -q  "^start\/group[[:space:]]\{1,\}astring[[:space:]]\{1,\}named" 2>/dev/null
if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s"  "Verify SMF start/timeout_seconds..."
svccfg -s svc:/network/dns/server:default listprop 2>/dev/null | /usr/xpg4/bin/grep -q  "^start\/timeout_seconds[[:space:]]\{1,\}count[[:space:]]\{1,\}240" 2>/dev/null
if [ $? -eq 0 ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s\n"  "Verify SMF man..."

svccfg -s svc:/network/dns/server listprop 2>/dev/null | /usr/xpg4/bin/grep -q "^tm_man_named\/title[[:space:]]\{1,\}astring[[:space:]]\{1,\}named" 2>/dev/null
if [ $? -eq 0 ]; then echo "	Success - title"; else echo "Failure - title"; fi

svccfg -s svc:/network/dns/server listprop 2>/dev/null | /usr/xpg4/bin/grep -q "^tm_man_named\/manpath[[:space:]]\{1,\}astring[[:space:]]\{1,\}\/opt\/bind\/share\/man" 2>/dev/null
if [ $? -eq 0 ]; then echo "	Success - path"; else echo "Failure - path"; fi

svccfg -s svc:/network/dns/server listprop 2>/dev/null | /usr/xpg4/bin/grep -q "^tm_man_named\/section[[:space:]]\{1,\}astring[[:space:]]\{1,\}8" 2>/dev/null
if [ $? -eq 0 ]; then echo "	Success - section"; else echo "Failure - section"; fi

printf "%-60s" "Verify commands dig, nslookup, host..."
if [ -L /usr/sbin/dig -a -L /usr/sbin/nslookup -a -L /usr/sbin/host ]; then echo "Success"; else echo "Failure"; fi

printf "%-60s" "Verify /etc/named, /etc/named/named.conf..."

if [ -f /etc/named/named.conf ] ;then
	echo "Success - /etc/named/named.conf is in place"
else
	if [ -d /etc/named ]; then
		echo "Failure - File etc/named/named.conf is missing"
	else
		echo "Failure - Directory /etc/named is missing"
	fi
	echo -e "\nVerification aborted!\n"
	exit 1
fi

printf "%-60s" "Verify tool zonecat is executable..."
if [ -x /usr/local/sbin/zonecat ]; then echo "Success"; else echo "Failure"; echo -e "\nVerification aborted!\n"; exit 1; fi


printf "%-60s\n" "Verify zone configuration files under /etc/named, DB directories..."
cd /etc/named
for file in root.zones base.zones its.zones mp.zones qa.zones pp.zones misc.zones nodb.zones; do

	if [ ! -f $file ]; then 
		echo "	Failure - /etc/named/${file} is missing"
	else 
		echo "	Success - /etc/named/${file} is in place"
	fi

	/usr/xpg4/bin/grep -q "^include \"/etc/named/${file}\";" /etc/named/named.conf
	if [ $? -eq 0 ]; then 
		echo "	Success - /etc/named/${file} is included on /etc/named/named.conf"
	else
		echo "	Failure - /etc/named/${file} is missing on /etc/named/named.conf"
	fi

	dir=$(echo $file | sed -e 's/\.zones//')
	if [ ! -d /var/named/db/${dir} ]; then 
		echo "	Failure - /var/named/db/${dir} is missing"
	else
		if [ "X$(ls /var/named/db/${dir})" == "X" ]; then
			echo "	Failure - /var/named/db/${dir} is empty"
		else
			echo "	Success - /var/named/db/${dir} is not empty"
		fi
	fi
done

/usr/local/sbin/zonecat its.zones  2>/dev/null | grep vip.its.ebay.com | /usr/xpg4/bin/grep -q master
if [ $? -ne 0 ]; then
   for file in bl.zones; do
	if [ ! -f $file ]; then 
		echo "	Failure - /etc/named/${file} is missing"
	else 
		echo "	Success - /etc/named/${file} is in place"
	fi

	/usr/xpg4/bin/grep -q "^include \"/etc/named/${file}\";" /etc/named/named.conf
	if [ $? -eq 0 ]; then 
		echo "	Success - /etc/named/${file} is included on /etc/named/named.conf"
	else
		echo "	Failure - /etc/named/${file} is missing on /etc/named/named.conf"
	fi

	dir=$(echo $file | sed -e 's/\.zones//')
	if [ ! -d /var/named/db/${dir} ]; then 
		echo "	Failure - /var/named/db/${dir} is missing"
	else
		if [ "X$(ls /var/named/db/${dir})" == "X" ]; then
			echo "	Failure - /var/named/db/${dir} is empty"
		else
			echo "	Success - /var/named/db/${dir} is not empty"
		fi
	fi
   done
else
   for file in bl.zones; do
	/usr/xpg4/bin/grep -q "^include \"/etc/named/${file}\";" /etc/named/named.conf
	if [ $? -eq 0 ]; then 
		echo "	Failure - /etc/named/${file} is included on /etc/named/named.conf"
	else
		echo "	Success - /etc/named/${file} is excluded on /etc/named/named.conf"
	fi
   done
	
	printf "%-60s" "Verify /.ssh/id_dsa & /.ssh/id_rsa..."
	if [ -f /.ssh/id_dsa -a -f /.ssh/id_rsa ]; then
		echo "Success"
	else
		echo "Failure"	
	fi
	
fi


printf "%-60s\n" "Verify /ebay/bind-logs..."
if [ -d /ebay/bind-logs ]; then
	echo "	Success - /ebay/bind-logs is in place"
	if ls -ald /ebay/bind-logs | /usr/xpg4/bin/grep -q "named[[:space:]]*named"; then
		echo "	Success - ownership is correct"
	else
		echo "	Failure - incorrect ownership"
	fi
else
	echo "	Failure - /ebay/bind-logs is missing"
fi

printf "Verify named status..."
if svcs -v svc:/network/dns/server:default |  /usr/xpg4/bin/grep -q ^online; then
	echo "	svc:/network/dns/server:default is online"
else
	echo "	error: svc:/network/dns/server:default is not online"
fi 


printf "%-20s\n" "named process:"
ps -ef | grep named | grep -v grep 

